package com.fc.happyteam.admin;

import com.fc.happyteam.common.ConverterUtil;
import com.fc.happyteam.common.ResponseJson;
import com.fc.happyteam.dto.admin.SysDictGroupDto;
import com.fc.happyteam.dto.admin.SysDictItemDto;
import com.fc.happyteam.entity.SysDictGroupEntity;
import com.fc.happyteam.entity.SysDictItemEntity;
import com.fc.happyteam.service.ISysDictService;
import com.fc.happyteam.utils.PageUtils;
import com.fc.happyteam.utils.Query;
import com.fc.happyteam.utils.R;
import com.fc.happyteam.utils.annotation.SysLog;
import com.fc.happyteam.utils.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/meta/dict")
public class SysDictController {
    @Autowired
    private ISysDictService sysDictService;

    /**
     * 所有配置列表
     */
    @RequestMapping("/group/list")
    public ResponseJson listGroup(@RequestParam Map<String, Object> params) {
        ResponseJson responseJson = new ResponseJson();

        //查询列表数据
        Query query = new Query(params);
        List<SysDictGroupEntity> sysDictGroupList = sysDictService.querySysDictGroupList(query);
        int total = sysDictService.querySysDictGroupTotal(query);

        List<SysDictGroupDto> sysDictGroupDtoList = ConverterUtil.convertList(SysDictGroupDto.class, sysDictGroupList);

        PageUtils pageUtil = new PageUtils(sysDictGroupDtoList, total, query.getLimit(), query.getPage());
        responseJson.setObj(pageUtil);

        return responseJson;
    }


    /**
     * 字典group信息
     */
    @RequestMapping("/group/info/{code}")
    public R groupInfo(@PathVariable("code") String code){
        SysDictGroupEntity sysDictGroupEntity = sysDictService.getSysDictGroupByCode(code);
        SysDictGroupDto sysDictGroupDto = ConverterUtil.convert(sysDictGroupEntity, SysDictGroupDto.class);
        return R.ok().put("group", sysDictGroupDto);
    }

    @SysLog("保存字典group")
    @RequestMapping("/group/save")
    public R saveGroup(@RequestBody SysDictGroupDto sysDictGroupDto) {
        SysDictGroupEntity sysDictGroupEntity = ConverterUtil.convert(sysDictGroupDto, SysDictGroupEntity.class);
        ValidatorUtils.validateEntity(sysDictGroupEntity);
        sysDictService.saveDictGroup(sysDictGroupEntity);
        return R.ok();
    }

    /**
     * 字典group信息
     */
    @RequestMapping("/item/info/{code}")
    public R itemInfo(@PathVariable("code") String code){
        SysDictItemEntity sysDictItemEntity = sysDictService.getSysDictItemByCode(code);
        SysDictItemDto sysDictItemDto = ConverterUtil.convert(sysDictItemEntity, SysDictItemDto.class);
        return R.ok().put("item", sysDictItemDto);
    }

    /**
     * 保存字典item
     */
    @SysLog("保存字典item")
    @RequestMapping("/item/save")
    public R saveItem(@RequestBody SysDictItemDto sysDictItemDto) {
        SysDictItemEntity sysDictItemEntity = ConverterUtil.convert(sysDictItemDto, SysDictItemEntity.class);
        ValidatorUtils.validateEntity(sysDictItemEntity);
        sysDictService.saveDictItem(sysDictItemEntity);

        return R.ok();
    }

    /**
     * 修改item
     */
    @RequestMapping("/item/update")
    public R updateItem(@RequestBody SysDictItemDto sysDictItemDto) {
        SysDictItemEntity sysDictItemEntity = ConverterUtil.convert(sysDictItemDto, SysDictItemEntity.class);
        ValidatorUtils.validateEntity(sysDictItemEntity);
        sysDictService.updateSysDictItem(sysDictItemEntity);
        return R.ok();
    }

    /**
     * 修改group
     */
    @RequestMapping("/group/update")
    public R updateGroup(@RequestBody SysDictGroupDto sysDictGroupDto) {
        SysDictGroupEntity sysDictGroupEntity = ConverterUtil.convert(sysDictGroupDto, SysDictGroupEntity.class);
        ValidatorUtils.validateEntity(sysDictGroupEntity);
        sysDictService.updateSysDictGroup(sysDictGroupEntity);
        return R.ok();
    }

    /**
     * 删除item
     */
    @RequestMapping("/item/delete")
    public R deleteItem(@RequestBody Integer[] ids) {
        sysDictService.deleteSysDictItems(ids);
        return R.ok();
    }

    /**
     * 删除group
     */
    @RequestMapping("/group/delete")
    public R deleteGroup(@RequestBody Integer[] ids) {
        sysDictService.deleteSysDictGroups(ids);
        return R.ok();
    }
}
